<template>
	<view class="repair">
		<view class="repair-input">
			<view class="online" @tap="onlineluru">
				<image src="../../static/pen.png" mode=""></image>
				<text>在线录入</text>
			</view>
			<!-- <input type="text" @tap="showHospitalList" v-model="hospital"
				placeholder-style="font-size: 28rpx;font-weight: 500;color: #C6C7CB;" placeholder="请选择客户名称" value="" />
			<view class="arrow">

			</view> -->
			<!-- 医院列表 -->
			<!-- <view class="hospital_list" v-if="hospitalShow">
				<view class="" v-for="i in hospitalList" :key='i.id' @tap.stop='choiceHospital(i.id,i.name)'>
					{{i.name}}
				</view>
			</view> -->
			<view class="fulNew">
				<view class="fulNew_left">
					*医院
				</view>
				<view class="fulNew_right">
					<xfl-select :list="list" :clearable="false" :showItemNum="5" :listShow="false" :isCanInput="true"
						:style_Container="'height: 50rpx; font-size: 16px;width:350rpx'" :placeholder="'请选择医院'"
						:focusShowList='"showList1Hospilal"' :selectHideType="'hideAll'" @change='hospitalEven()'
						@input='manualSerachHospital()'>
					</xfl-select>
				</view>
			</view>
			<view class="fulNew">
				<view class="fulNew_left">
					*科室
				</view>
				<view class="fulNew_right">
					<xfl-select :list="list1" :clearable="false" :showItemNum="5" :listShow="false" :isCanInput="true"
						:focusShowList='"showList2Hospilal"'
						:style_Container="'height: 50rpx; font-size: 16px;width:350rpx'" :placeholder="'请选择科室'"
						@change='departmentEven()' @input='manualSerachDepartment()'>
					</xfl-select>
				</view>
			</view>
			<view class="fulNew">
				<view class="fulNew_left">
					*设备名称
				</view>
				<view class="fulNew_right">
					<xfl-select :list="list2" :clearable="false" :showItemNum="5" :listShow="false" :isCanInput="true"
						:focusShowList='"showList3Hospilal"'
						:style_Container="'height: 50rpx; font-size: 16px;width:350rpx'" :placeholder="'请选择设备'"
						:selectHideType="'hideAll'" @change='equipmentEven()' @input='manualSerachEquipment()'>
					</xfl-select>
				</view>
			</view>
			<view class="fulNew">
				<view class="fulNew_left">
					*设备序号
				</view>
				<view class="fulNew_right">
					<xfl-select :list="list3" :clearable="false" :showItemNum="5" :listShow="false" :isCanInput="true"
						:focusShowList='"showList3Hospilal"'
						:style_Container="'height: 50rpx; font-size: 16px;width:350rpx'" :placeholder="'请选择设备序号'"
						:selectHideType="'hideAll'" @change='serialNoEven()' @input='serialNoEquipment()'>
					</xfl-select>
				</view>
			</view>
			<view class="fulNew">
				<view class="fulNew_left">
					医院联系人
				</view>
				<view class="fulNew_right" style="width: 400rpx;">
					<input type="text" v-model="contacts" style="padding: 0rpx;" value="" placeholder="请输入医院联系人姓名" />
				</view>
			</view>
			<view class="fulNew">
				<view class="fulNew_left">
					医院联系方式
				</view>
				<view class="fulNew_right" style="width: 400rpx;">
					<input type="text" v-model="mobile" style="padding: 0rpx;" value="" placeholder="请输入医院联系方式" />
				</view>
			</view>

		</view>
		<view class="repair-m">
			<view class="repair-m-n">
				设备信息
			</view>
			<view class="repair-m-f">
				<view class="repair-m-f-n">
					*序号
				</view>
				<view class="repair-m-f-i">
					<!-- <input type="text" v-model="product"
						placeholder-style="font-size:28rpx;line-height:34rpx;color:#B6BDBD;" placeholder="请输入产品名称"
						value="" /> -->
					{{equipmentData.serial||'未更新'}}
			
				</view>
			</view>
			<view class="repair-m-f">
				<view class="repair-m-f-n">
					*型号
				</view>
				<view class="repair-m-f-i">
					<!-- <input type="text" v-model="product"
						placeholder-style="font-size:28rpx;line-height:34rpx;color:#B6BDBD;" placeholder="请输入产品名称"
						value="" /> -->
					{{equipmentData.marking||'未更新'}}

				</view>
			</view>
			<view class="repair-m-f">
				<view class="repair-m-f-n">
					*软件版本
				</view>
				<view class="repair-m-f-i">
					<!-- <input type="text" v-model="device"
						placeholder-style="font-size:28rpx;line-height:34rpx;color:#B6BDBD;" placeholder="请输入设备型号"
						value="" /> -->
					{{equipmentData.software_version||'未更新'}}
				</view>
			</view>
			<view class="repair-m-f">
				<view class="repair-m-f-n">
					附件
				</view>
				<view class="repair-m-f-i">
					<!-- <input type="text" placeholder-style="font-size:28rpx;line-height:34rpx;color:#B6BDBD;"
						placeholder="选填" value="" /> -->
					{{equipmentData.enclosure||'未更新'}}
				</view>
			</view>
			<!-- <view class="repair-m-f">
				<view class="repair-m-f-n">
					软件版本
				</view>
				<view class="repair-m-f-i">
					<input type="text" v-model="software_version"
						placeholder-style="font-size:28rpx;line-height:34rpx;color:#B6BDBD;" placeholder="选填"
						value="" />
				</view>
			</view> -->
		</view>

		<view class="repair-w">
			<view class="repair-w-n">
				问题描述
			</view>
			<view class="repair-w-q">
				<view class="repair-w-q-t" style="padding-bottom:20rpx">
					<textarea value="" v-model="describe"
						placeholder-style="font-size:28rpx;line-height:34rpx;color:#B6BDBD;" style="height:120rpx"
						placeholder="请简短描述您的问题" />
				</view>
				<view class="repair-w-q-i">
					<view class="repair-w-q-i-a" v-for="(item,index) in imgs_list" :key='index'>
						<image :src="item" mode="" style="width: 100%;height: 100%;z-index: 99;"></image>
						<image src="../../static/del.png" class="del" mode="" @tap="del_img(index)"></image>
					</view>
					<!-- 	<view class="repair-w-q-i-b">
					</view> -->
					<view class="repair-w-q-i-c" @tap="upImgs">
						<image src="../../static/camera.png" class="img" mode=""></image>
					</view>
				</view>
				<view class="repair-w-q-p">
					支持上传2张jpg/png
				</view>
			</view>
		</view>
		<view class="submit" @tap="btnJump">
			提交
		</view>
	</view>
</template>

<script>
	import config from '@/request/config.js'
	import xflSelect from '../../components/xfl-select/xfl-select.vue';
	export default {
		components: {
			xflSelect //注册为子组件
		},
		data() {
			return {
				showList1Hospilal: false, //医院自动弹框
				showList2Hospilal: false, //科室自动弹框
				showList3Hospilal: false, //设备自动弹框
				showList4Hospilal: false, //设备序号自动弹框
				imgs_list: [], // 附件图片
				describe: '', //问题描述
				contacts: '', //联系人,
				mobile: '', //手机号
				departmentList_id: null, //科室id
				hospital_id: null, //医院id
				software_version: '', //系统版本
				hospitalList: [], //医院列表
				list: [], //医院下拉框列表
				departmentList: [], //科室列表
				list1: [ //科室要展示的数据
				],
				equipmentList: [], //设备列表
				list2: [ //设备要展示的数据
				],
				serialNoList: [], //设备序号列表
				list3:[ //序号要展示的数据
				],
				equipment_id: null, //设备id
				equipmentData: {} ,//设备信息
				serialNo_id: null // 序号id
			}
		},
		onLoad() {
			this.getHospital()
		},
		methods: {
			/* 在线录入 */
			onlineluru() {
				uni.navigateTo({
					url: './onlinecreate'
				})
			},

			/* 获取医院列表 */
			getHospital() {
				this.$https.post('/hospital/search', {
					name: ''
				}).then(res => {
					this.hospitalList = res.data
					this.hospitalList.forEach(item => {
						this.list.push(item.name)
					})
				})
			},
			/* 获取科室列表 */
			getDepartment(id) {
				this.$https.post('/phase/getDepartment', {
					hospital_id: id
				}).then(res => {
					console.log(res, 15);
					this.departmentList = res.data
					this.departmentList.forEach(item => {
						this.list1.push(item.name)
					})
				})
			},
			/* 获取设备列表 */
			getEquipmentList(id) {
				this.$https.post('/phase/getEquipmentList', {
					department_id: id
				}).then(res => {
					this.equipmentList = res.data
					this.equipmentList.forEach(item => {
						this.list2.push(item.name)
					})
				})
			},
			/* 获取设备序号列表 */
			getSerialNoList(id,sid){// id 科室   sid  设备
				this.$https.post('/phase/getEquipmentList2', {
					department_id: id,
					name: sid
				}).then(res => {
					this.serialNoList = res.data
					this.serialNoList.forEach(item => {
						this.list3.push(item.name)
					})
				})
			},
			/* 手动搜索医院 */
			manualSerachHospital(e) {
				this.list1 = []
				this.list2 = []
				this.equipment_id = null
				this.equipmentData = {}
				this.$children[2].onClear()
				this.$children[1].onClear()
				this.$https.post('/hospital/search', {
					name: e.detail.value
				}).then(res => {
					this.hospitalList = res.data
					this.list = this.hospitalList.map(item => {
						return item.name
					})
					this.showList1Hospilal = true
				})
			},
			/* 手动搜索科室 */
			manualSerachDepartment(e) {
				this.list2 = []
				this.equipment_id = null
				this.equipmentData = {}
				this.$children[2].onClear()
				this.$https.post('/phase/getDepartment', {
					name: e.detail.value,
					hospital_id: this.hospital_id
				}).then(res => {
					console.log(res, 147);
					this.departmentList = res.data
					this.list1 = this.departmentList.map(item => {
						return item.name
					})
					this.showList2Hospilal = true
				})
			},
			/* 手动搜索设备 */
			manualSerachEquipment(e) {
				this.$https.post('/phase/getEquipmentList', {
					name: e.detail.value,
					department_id: this.departmentList_id,
				}).then(res => {
					this.equipmentList = res.data
					this.list2 = this.equipmentList.map(item => {
						return item.name
					})
					this.showList3Hospilal = true
				})
			},
			/* 手动搜索设备序号 */
			serialNoEquipment(e){
				this.$https.post('/phase/getEquipmentList2', {
					department_id: this.departmentList_id,
					name: this.equipment_id,
					serial: e.detail.value
				}).then(res => {
					console.log(res)
					this.serialNoList = res.data
					this.list3 = this.serialNoList.map(item => {
						return item.name
					})
					this.showList4Hospilal = true
				})
			},
			/* 医院列表事件 */
			hospitalEven(e) {
				this.list1 = []
				this.list2 = []
				this.list3 = []
				this.equipment_id = null
				this.equipmentData = {}
				this.$children[3].onClear()
				this.$children[2].onClear()
				this.$children[1].onClear()
				let de_data = this.hospitalList.find(item => {
					return item.name == e.newVal
				})
				this.hospital_id = de_data.id
				this.getDepartment(this.hospital_id)
			},
			/* 科室事件 */
			departmentEven(e) {
				this.list2 = []
				this.list3 = []
				this.equipment_id = null
				this.equipmentData = {}
				this.$children[3].onClear()
				this.$children[2].onClear()
				let de_data = this.departmentList.find(item => {
					return item.name == e.newVal
				})
				this.departmentList_id = de_data.id
				this.getEquipmentList(this.departmentList_id)
			},
			/* 设备事件 获取设备详情 */
			equipmentEven(e) {
				this.list3 = []
				this.equipmentData = {}
				this.$children[3].onClear()
				let de_data = this.equipmentList.find(item => {
					return item.name == e.newVal
				})
				this.equipment_id = de_data.id
				this.getSerialNoList(this.departmentList_id,this.equipment_id)
				// this.getEquipmentDetails(this.equipment_id)
			},
			/* 设备序号事件 */
			serialNoEven(e){
				console.log(e)
				let de_data = this.serialNoList.find(item => {
					return item.name == e.newVal
				})
				this.serialNo_id = de_data.id
				this.getEquipmentDetails(this.serialNo_id)
			},
			/* 获取设备信息 */
			getEquipmentDetails(id) {
				this.$https.post('/phase/getEquipment', {
					id: id
				}).then(res => {
					this.equipmentData = res.data
				})
			},
			

			/* 删除照片 */
			del_img(i) {
				this.imgs_list.splice(i, 1)
			},
			/* 图片上传 */
			upImgs() {
				const token = uni.getStorageSync('userInfo').token
				uni.chooseImage({
					count: 2, // 最多可以选择的图片张数，默认9
					sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
					success: res => {
						/* 	console.log(res, 3336);
							this.imgs_list = res.tempFilePaths */

						res.tempFilePaths.forEach(item => {
							uni.uploadFile({
								url: config.baseUrl + '/common/upload',
								filePath: item,
								name: 'file',
								formData: {
									token: token // 上传附带参数
								},
								success: (res1) => {
									console.log(res1);

									if (res1.data.code == 0) {
										uni.showToast({
											title: res1.data.msg,
											duration: 1500,
											icon: 'error'
										})
									} else {
										let img = JSON.parse(res1.data).data.fullurl

										this.imgs_list.push(img)
									}


									/* let img = JSON.parse(res1.data).data.fullurl

									this.imgs_list.push(img) */
								},
								fail: res3 => {
									uni.showToast({
										title: '上传失败',
										duration: 1500,
										icon: 'error'
									})
								}
							});
						})
					},
					fail: (res) => {
						uni.showToast({
							title: '上传失败',
							duration: 1500,
							icon: 'error'
						})
						console.log(res, 33);
					}
				})
			},
			checkMobile() {
				if (!(/^1[3456789]\d{9}$/.test(13558881543))) {
					uni.showToast({
						icon: 'none',
						title: '请输入正确的电话号码',
						duration: 1500
					})
					this.mobile = ''
					return false
				}

			},
			isMobile(mobile) {
				return /^1[3-9]\d{9}$/.test(mobile)
			},
			/* 创建工单 */
			btnJump() {
				if (!this.isMobile(this.mobile)) {
					uni.showToast({
						icon: 'none',
						title: '请输入正确的电话号码',
						duration: 1500
					})
					this.mobile = ''
				} else if ( !this.contacts) {
					uni.showToast({
						icon: 'none',
						title: '请输入联系人姓名',
						duration: 1500
					})
				} else {
					let imgs= this.imgs_list.toString()
					let params = {
						describe: this.describe,
						hospital_id: this.hospital_id,
						software_version: this.software_version,
						images: imgs,
						equipment_id: this.serialNo_id,
						// equipment_id: this.equipment_id,
						contacts: this.contacts,
						mobile: this.mobile,
					}
					this.$https.post('/warranty/add', params).then(res => {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 1500
						})
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							})
						}, 1500)
					})
				}

			}

		}
	}
</script>

<style lang="scss" scoped>
	.online {
		display: flex;
		align-items: center;
		z-index: 9999999999999999999999999;
		position: fixed;
		right: 0rpx;
		top: 600rpx;
		font-size: smaller;
		background-color: #1a66ff;
		color: #FFFFFF;
		padding: 10rpx 5rpx 10rpx 20rpx;
		border-radius: 20rpx 0 0 20rpx;

		image {
			width: 32rpx;
			height: 32rpx;
			margin-right: 15rpx;
		}
	}

	.fulNew {
		display: flex;
		justify-content: space-between;
		height: 100rpx;
		line-height: 100rpx;
		color: #3B3D3D;
		font-weight: 500;
		border-bottom: 2rpx solid #f6f7fb;
		background-color: #fff;
		font-size: 28rpx
	}

	.del {
		position: absolute;
		width: 32rpx;
		height: 32rpx;
		top: 0rpx;
		right: 0rpx;
		border-radius: 50% !important;
		background-color: #FFFFFF;
		z-index: 99999;
	}

	.submit {
		width: 622rpx;
		height: 80rpx;
		border-radius: 80rpx;
		background-color: $public-oclor;
		line-height: 80rpx;
		text-align: center;
		color: #FFFFFF;
		margin: 30rpx auto;
	}

	.repair-input {
		padding: 20rpx 32rpx;
		background-color: #f6f7fb;
		position: relative;
		background-color: #fff;

		.arrow {
			position: absolute;
			right: 60rpx;
			top: 50rpx;
			width: 0;
			height: 0;
			border-left: 15rpx solid transparent;
			border-right: 15rpx solid transparent;
			border-top: 30rpx solid #C6C7CB;
		}

		.hospital_list {
			position: absolute;
			z-index: 99;
			display: inline-block;
			box-sizing: border-box;
			top: 110rpx;
			height: 225rpx;
			overflow: hidden;
			border: 2rpx solid #f6f7fb;
			width: 90%;
			overflow-y: scroll;
			background-color: #FFFFFF;
			border-radius: 4rpx;

			view {
				padding-left: 20rpx;
				color: #C6C7CB;
				font-size: 24rpx;
				line-height: 42rpx;
			}
		}

		input {
			padding: 0 100rpx 0 50rpx;
			height: 88rpx;
			border-radius: 88rpx;
			background-color: #FFFFFF;
		}
	}

	.repair {
		&-w {
			border-bottom: 20rpx solid #f6f7fb;

			&-q {
				padding: 58rpx 32rpx;

				&-p {
					font-size: 28rpx;
					color: #B6BDBD;
					line-height: 33rpx;
					margin-top: 40rpx;
				}

				&-i {
					display: flex;
					padding: 40rpx 0;

					&-a {
						width: 160rpx;
						height: 160rpx;
						background: #F5F6F8;
						margin-right: 20rpx;
						border-radius: 4rpx;
						position: relative;

						image {
							border-radius: 4rpx;
						}
					}

					&-b {
						width: 160rpx;
						height: 160rpx;
						background: #F5F6F8;
						margin: 0 16rpx;
					}

					&-c {
						width: 160rpx;
						height: 160rpx;
						background: #F5F6F8;
						position: relative;

						.img {
							width: 60rpx;
							height: 60rpx;
							position: absolute;
							left: 50%;
							top: 50%;
							margin-top: -30rpx;
							margin-left: -30rpx;
						}

						image {}
					}
				}

				&-t {
					border-bottom: 2rpx solid #f6f7fb;

					textarea {
						width: 100%;
					}
				}
			}

			&-n {
				padding: 20rpx 32rpx;
				background: linear-gradient(91deg, #e2ecfb 0%, rgba(255, 255, 255, 0) 100%);
				font-size: 28rpx;
				font-weight: 600;
				color: #6AA6FA;
				line-height: 33rpx;
			}
		}

		&-m {
			border-bottom: 20rpx solid #f6f7fb;

			&-n {
				padding: 20rpx 32rpx;
				background: linear-gradient(86deg, rgba(226, 140, 129, 0.15) 0%, rgba(226, 140, 129, 0) 100%);
				font-size: 28rpx;
				font-weight: 600;
				color: #E98D78;
				line-height: 33rpx;
			}

			&-f {
				margin: 0 25rpx;
				padding: 32rpx 0;
				display: flex;
				justify-content: space-between;
				border-bottom: 2rpx solid #f6f7fb;
				align-items: center;

				&-n {
					font-size: 28rpx;
					font-weight: 500;
					color: #3B3D3D;
					line-height: 33rpx;
				}

				&-i {
					font-size: 28rpx;
					font-weight: 500;
					color: #3B3D3D;
					line-height: 33rpx;
				}
			}
		}
	}
</style>
